// 将请求时的参数对象转换为 uri 字符串
// paramsObj = {page: 1, pre_page: 10}; => "page=1&pre_page=10"
export function convertObjToURI(paramsObj) {
  return Object.keys(paramsObj)
    .map((k) => encodeURIComponent(k) + '=' + encodeURIComponent(paramsObj[k]))
    .join('&')
}

//用来检测数据类型的方法
export function checkType(type) {
  return function (context) {
    return Object.prototype.toString.call(context) === `[object ${type}]`
  }
}
//判断是否为字符串
export const isString = checkType('String')
//判断是否为数字
export const isNumber = checkType('Number')
//判断是否为boolean
export const isBoolean = checkType('Boolean')
//判断是否为Null
export const isNull = checkType('Null')
//判断是否为Undefined
export const isUndefined = checkType('Undefined')
//判断是否为Function
export const isFunction = checkType('Function')
//判断是否为json对象
export const isJson = function (obj) {
  return typeof obj == 'object' && Object.prototype.toString.call(obj).toLowerCase() == '[object object]' && !obj.length
}
// 移除两侧空格
export function trim(str) {
  return str.replace(/^\s+|\s+$/g, '')
}

//用来生成uuid的方法
export function uuid(len = 35, r) {
  let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  let uuid = []
  let i
  let radix = r || chars.length

  if (len) {
    for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]
  } else {
    let r
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
    uuid[14] = '4'
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | (Math.random() * 16)
        uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r]
      }
    }
  }
  return uuid.join('')
}

//将数字保留2位小数
export function getToFixedNumber(num) {
  // num = !isNaN(num) ? Number(num) : 0
  // return Number(num).toFixed(2)
  return Number(Number(num) || 0).toFixed(2)
}
